home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 426-450 / disk_442 / toolmanager / window.c < prev   
C/C++ Source or Header  |  1992-05-06  |  9KB  |  280 lines

  1. #include "ToolManager.h"
  2.  
  3. /* Structures for window */
  4. extern UBYTE WindowTitle[];
  5. extern struct NewWindow nw;
  6. #define WDRAGBARLEN 60       /* Width of Close & Drag gadget */
  7. static struct Window *w=NULL;
  8. static struct AppWindow *aw;
  9. static struct MsgPort *wp;
  10. static ULONG wl=20,wt=20;    /* Window leftedge & topedge */
  11. static ULONG ww,wh;          /* Window width & height */
  12. static struct Screen *pubsc; /* Workbench screen */
  13.  
  14. /* Structures for window gadgets */
  15. static void *vi;             /* Visual information is a *PRIVATE* data field! */
  16. static struct Gadget *gl;    /* Gadget list */
  17. static char StatusText[]="Active Tools: 00"; /* Text gadget text */
  18. static struct Gadget *lvgad;                 /* ListView gadget */
  19. static WORD lvord=-1;                        /* LV gadget ordinal number */
  20. #define LVGAD_ID 1
  21. static char RTButtonText[]="Remove Tool";    /* Remove Tool gadget text */
  22. #define RTGAD_ID 2
  23. static char SCButtonText[]="Save Configuration"; /* Save config gadget test */
  24. #define SCGAD_ID 3
  25.  
  26. /* Create status line */
  27. static void PrintStatusLine(void)
  28. {
  29.  StatusText[14]=ToolCount/10+'0'; /* Hack */
  30.  StatusText[15]=ToolCount%10+'0';
  31. }
  32.  
  33. /* Create all status window gadgets */
  34. static BOOL CreateWindowGadgets(void)
  35. {
  36.  struct NewGadget ng;
  37.  struct Gadget *g;
  38.  struct TextAttr *ta=pubsc->Font;
  39.  struct TextFont *f;              /* Window font */
  40.  struct RastPort tmprp;           /* RastPort for font-sensitive trick */
  41.  UWORD width,fheight;
  42.  UWORD leftedge,topborder;
  43.  UWORD lvright;
  44.  
  45.  /* Open window font */
  46.  if (!(f=OpenFont(ta))) return(FALSE);
  47.  fheight=f->tf_YSize; /* Font height */
  48.  leftedge=pubsc->WBorLeft;
  49.  topborder=pubsc->WBorTop+fheight+1;
  50.  
  51.  /* Initialize temporary RastPort */
  52.  InitRastPort(&tmprp);
  53.  SetFont(&tmprp,f);
  54.  
  55.  /* Calculate window width */
  56.  ww=TextLength(&tmprp,WindowTitle,strlen(WindowTitle))+WDRAGBARLEN;
  57.  
  58.  /* Create gadget list */
  59.  gl=NULL;
  60.  g=CreateContext(&gl);
  61.  
  62.  /* 1. gadget: Text, status line */
  63.  PrintStatusLine();
  64.  width=TextLength(&tmprp,StatusText,sizeof(StatusText)-1)+INTERWIDTH;
  65.  ng.ng_LeftEdge=leftedge+(ww-width)/2;
  66.  ng.ng_TopEdge=topborder+INTERHEIGHT;
  67.  ng.ng_Width=width;
  68.  ng.ng_Height=fheight;
  69.  ng.ng_GadgetText=StatusText;
  70.  ng.ng_TextAttr=ta;
  71.  ng.ng_Flags=PLACETEXT_IN;
  72.  ng.ng_VisualInfo=vi;
  73.  ng.ng_UserData=0;
  74.  g=CreateGadget(TEXT_KIND,g,&ng,TAG_DONE);
  75.  g->GadgetText->DrawMode=JAM2; /* Argh, why doesn't exist a tag for this? */
  76.  
  77.  /* 2. gadget: ListView, tool list */
  78.  ng.ng_LeftEdge=leftedge+WDRAGBARLEN/2;
  79.  ng.ng_TopEdge+=g->Height+INTERHEIGHT;
  80.  ng.ng_Width=ww-WDRAGBARLEN;
  81.  ng.ng_Height=7*fheight;
  82.  ng.ng_GadgetText=NULL;
  83.  ng.ng_GadgetID=LVGAD_ID;
  84.  lvgad=g=CreateGadget(LISTVIEW_KIND,g,&ng,
  85.                       GTLV_Labels,&ToolList,
  86.                       /* If an item was selected, use it as top item */
  87.                       GTLV_Top,lvord<0?0:lvord,
  88.                       GTLV_ShowSelected,NULL,
  89.                       GTLV_Selected,lvord,
  90.                       TAG_DONE);
  91.  lvright=ng.ng_LeftEdge+g->Width;
  92.  
  93.  /* 3. gadget: Button, remove tool */
  94.  ng.ng_TopEdge+=g->Height+fheight+2*INTERHEIGHT;
  95.  ng.ng_Width=TextLength(&tmprp,RTButtonText,sizeof(RTButtonText)-1)+INTERWIDTH;
  96.  ng.ng_Height=fheight+INTERHEIGHT;
  97.  ng.ng_GadgetText=RTButtonText;
  98.  ng.ng_GadgetID=RTGAD_ID;
  99.  g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE);
  100.  
  101.  /* 4. gadget: Button, save config */
  102.  width=TextLength(&tmprp,SCButtonText,sizeof(SCButtonText)-1)+INTERWIDTH;
  103.  ng.ng_LeftEdge=lvright-width;
  104.  ng.ng_Width=width;
  105.  ng.ng_GadgetText=SCButtonText;
  106.  ng.ng_GadgetID=SCGAD_ID;
  107.  g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE);
  108.  
  109.  /* Calculate window height */
  110.  wh=ng.ng_TopEdge+g->Height+INTERHEIGHT-topborder;
  111.  
  112.  /* Close window font */
  113.  CloseFont(f);
  114.  
  115.  /* All gadgets created! */
  116.  if (g) return(TRUE);
  117.  
  118.  /* Something went wrong.... */
  119.  FreeGadgets(gl);
  120.  return(FALSE);
  121. }
  122.  
  123. /* Open status window */
  124. ULONG OpenStatusWindow(void)
  125. {
  126.  if (!(pubsc=LockPubScreen("Workbench"))) /* Lock Workbench screen */
  127.   goto osw1;
  128.  
  129.  if (!(vi=GetVisualInfo(pubsc,TAG_DONE))) /* Get visual information */
  130.   goto osw2;
  131.  
  132.  if (!CreateWindowGadgets())              /* Create Gadgets */
  133.   goto osw3;
  134.  
  135.  /* Open window */
  136.  if (!(w=OpenWindowTags(&nw,
  137.                         WA_Left,wl,
  138.                         WA_Top,wt,
  139.                         WA_InnerWidth,ww,
  140.                         WA_InnerHeight,wh,
  141.                         WA_PubScreen,pubsc,
  142.                         WA_AutoAdjust,TRUE,
  143.                         TAG_DONE)))
  144.   goto osw4;
  145.  wp=w->UserPort; /* Retrieve window port */
  146.  
  147.  /* Circumvent an intuition.library bug. See AutoDocs for LockPubScreen() */
  148.  UnlockPubScreen(NULL,pubsc);
  149.  pubsc=NULL;
  150.  
  151.  /* Notify Workbench about window */
  152.  if (!(aw=AddAppWindow(NULL,NULL,w,MyMP,NULL)))
  153.   goto osw5;
  154.  
  155.  /* Add gadget list to window */
  156.  AddGList(w,gl,(UWORD) -1,(UWORD) -1,NULL);
  157.  RefreshGList(gl,w,NULL,(UWORD) -1);
  158.  GT_RefreshWindow(w,NULL);
  159.  
  160.  /* Window open! */
  161.  return(1L<<wp->mp_SigBit);
  162.  
  163.  /* Something went wrong.... */
  164. osw5: CloseWindow(w);
  165.       w=NULL;
  166. osw4: FreeGadgets(gl);
  167. osw3: FreeVisualInfo(vi);
  168. osw2: if (pubsc) UnlockPubScreen(NULL,pubsc);
  169. osw1: return(0);
  170. }
  171.  
  172. /* Refresh status window gadgets */
  173. void RefreshStatusWindow(void)
  174. {
  175.  PrintStatusLine();
  176.  RefreshGList(gl,w,NULL,2); /* Refresh only the first two gadgets */
  177. }
  178.  
  179. /* If the window is open, detach tool list from ListView gadget */
  180. void DetachToolList(void)
  181. {
  182.  if (w) GT_SetGadgetAttrs(lvgad,w,NULL,GTLV_Labels,-1,TAG_DONE);
  183. }
  184.  
  185. /* If the window is open, attach tool list to ListView gadget */
  186. void AttachToolList(void)
  187. {
  188.  if (w) GT_SetGadgetAttrs(lvgad,w,NULL,
  189.                           GTLV_Labels,&ToolList,
  190.                           /* If an item was selected, use it as top item */
  191.                           GTLV_Top,lvord<0?0:lvord,
  192.                           GTLV_Selected,lvord,
  193.                           TAG_DONE);
  194. }
  195.  
  196. /* Handle window events */
  197. BOOL HandleWindowEvent(void)
  198. {
  199.  BOOL rc=FALSE;             /* True if window close event */
  200.  struct IntuiMessage *msg;
  201.  struct ToolNode *tn;
  202.  FILE *fh;
  203.  
  204.  while (msg=GT_GetIMsg(wp)) /* Pre-process Intuition messages */
  205.   {
  206.    switch (msg->Class)
  207.     {
  208.      case INTUITICKS:       /* Intuition tick received */
  209.       break;
  210.      case CLOSEWINDOW:      /* User clicked the close gadget */
  211.       wl=w->LeftEdge;       /* Retreive window parameters for next open */
  212.       wt=w->TopEdge;
  213.       rc=TRUE;              /* Yes, close window */
  214.       break;
  215.      case REFRESHWINDOW:    /* Window must be refreshed */
  216.       GT_BeginRefresh(w);
  217.       GT_EndRefresh(w,TRUE);
  218.       break;
  219.      case GADGETUP:         /* User released a gadget */
  220.       switch(((struct Gadget *) msg->IAddress)->GadgetID)
  221.        {
  222.         case LVGAD_ID:      /* User selected a ListView item */
  223.          lvord=msg->Code;   /* Retrieve the ordinal number of the item */
  224.          break;
  225.         case RTGAD_ID:      /* User selected the remove tool gadget */
  226.          if (lvord>=0)      /* Is the ordinal number valid? */
  227.           {                 /* Yes, search tool and remove it */
  228.            WORD i=0;        /* Counter */
  229.  
  230.            /* Scan tool list until the ordinal number is reached */
  231.            for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn),i++)
  232.             if (i>=lvord) break;
  233.            lvord=-1;        /* invalidate ordinal number */
  234.  
  235.            /* Remove tool from list */
  236.            DetachToolList();
  237.            RemToolNode(tn);
  238.            AttachToolList();
  239.  
  240.            /* Refresh Gadgets */
  241.            RefreshStatusWindow();
  242.           }
  243.          break;
  244.         case SCGAD_ID:      /* User selected the save config gadget */
  245.          if (fh=fopen(ConfigName,"w"))      /* Open config file */
  246.           {
  247.            for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn))
  248.             {
  249.              fputs(tn->tn_Node.ln_Name,fh); /* Write menu entry name */
  250.              if (tn->tn_RealName)           /* Exists a real name? */
  251.               {
  252.                fputs(";",fh);               /* Yes, append it */
  253.                fputs(tn->tn_RealName,fh);
  254.               }
  255.              fputs("\n",fh);                /* Append a new line */
  256.             }
  257.            fclose(fh);                      /* Close the config file */
  258.           }
  259.          else                               /* Could not open config file! */
  260.           DisplayBeep(NULL);                /* --> Flash display           */
  261.          break;
  262.        }
  263.       break;
  264.     }
  265.    GT_ReplyIMsg(msg); /* Reply pre-processed message */
  266.   }
  267.  
  268.  return rc;
  269. }
  270.  
  271. /* Close status window */
  272. void CloseStatusWindow(void)
  273. {
  274.  RemoveAppWindow(aw);
  275.  CloseWindow(w);
  276.  w=NULL;
  277.  FreeGadgets(gl);    /* Release allocated resources */
  278.  FreeVisualInfo(vi);
  279. }
  280.